package cetcbigdata.da.zhengwugongkai;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static cetcbigdata.da.zhengwugongkai.util.*;

public class SpiderHBZW extends Base {
@Override
  public void getData(RecordSender recordSender, List<Object> fieldsList) {

      }

  @Override
  public JSONObject getDataDemo() {
          try {
              return this.parseListPage();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }

  private JSONObject parseListPage() throws IOException {

          //1.访问大的列表页
      String BigListUrl = "http://zwfw.hubei.gov.cn/web/bszn/zwgk/action/list?regionCode=420000000000";
      JSONObject BigHeaders = new JSONObject();
      JSONObject BigParmas = new JSONObject();
      String BiglistResponse = getReq(BigListUrl, BigHeaders, BigParmas);
      JSONObject BigresponseObj = JSONObject.parseObject(BiglistResponse);
      if (BigresponseObj.getString("status").equals("200")) {
         JSONArray Biglist = BigresponseObj.getJSONArray("data");
         //这里要进行分类讨论对每个网页的处理方式是不一样的
              for (Object o : Biglist) {
                  String BigLabel = ((JSONObject) o).getString("name");
                  String orderSort = ((JSONObject) o).getString("orderSort");
                  //orderSort为1代表访问权力清单
                  if (orderSort.equals("1")) {
                      int taskType = 1;
                      while (taskType != 12) {
                          String TaskType = ParameterFormat(taskType);
                          System.out.println(TaskType);
                          ++taskType;
                          int pages = 0;
                          int currentPage = 1;
                          while (pages != currentPage) {
                              System.out.println(currentPage);
                              String listUrl = "http://zwfw.hubei.gov.cn/web/bszn/zwgk/qzqd/getQzqdItemList.jspc";
                              JSONObject parmas = new JSONObject();
                              parmas.put("regionCode", "420000000000");
                              parmas.put("taskType", TaskType);
                              parmas.put("grade", "2");
                              parmas.put("currentPage", currentPage);
                              parmas.put("pageSize", "10");
                              String listResponse = postFormReq(listUrl, new JSONObject(), parmas);
                              JSONObject responseObj = JSONObject.parseObject(listResponse);
                              if (responseObj.getString("status").equals("200")) {
                                  pages = responseObj.getJSONObject("data").getJSONObject("pageList").getInteger("pages");
                                  JSONArray list = responseObj.getJSONObject("data").getJSONObject("pageList").getJSONArray("records");
                                  for (Object o1 : list) {    //获取必要信息
                                      String TASKCODE = ((JSONObject) o1).getString("TASKCODE");
                                      String TASKNAME = ((JSONObject) o1).getString("TASKNAME");
                                      //getQzqdSJYWList.jspc 访问该网页从而获取TRANSACTCODE
                                      String MiddleListUrl = "http://zwfw.hubei.gov.cn/web/bszn/zwgk/qzqd/getQzqdSJYWList.jspc";
                                      JSONObject MiddleParmasnext = new JSONObject();
                                      MiddleParmasnext.put("taskcode", TASKCODE);
                                      String listResponsenext = postFormReq(MiddleListUrl, new JSONObject(),MiddleParmasnext);
                                      JSONObject responseObjnext = JSONObject.parseObject(listResponsenext);
                                      JSONArray transactCodeList = responseObjnext.getJSONObject("data").getJSONArray("records");
                                      for (Object o2 : transactCodeList) {
                                          String TRANSACTCODE = ((JSONObject) o2).getString("TRANSACTCODE");
                                          //开始解析详情页

                                          JSONObject detailmessage = new JSONObject();

                                          JSONObject content = this.parseLegalbasis(TRANSACTCODE);
                                          detailmessage.put("设定依据", content);

                                          //content = this.parseLingprocess(TRANSACTCODE);
                                          //detailmessage.put("办理流程", content);

                                          content = this.parsePagecondition(TRANSACTCODE);
                                          detailmessage.put("受理条件", content);

                                          //用来将多个jsonobject对象进行组合---------------------------
                                          JSONArray jsonArray = new JSONArray();

                                          content = this.parseInfoExt(TRANSACTCODE);
                                          jsonArray.add(content);
                                          //detailmessage.put("基本信息",content);

                                          content = this.parseDocument(TRANSACTCODE);
                                          jsonArray.add(content);
                                          //detailmessage.put("审批结果信息",content);

                                          content = this.parseProcessingInformation(TRANSACTCODE);
                                          jsonArray.add(content);
                                          //detailmessage.put("办理信息",content);

                                          content = this.parseOther(TRANSACTCODE);
                                          jsonArray.add(content);
                                          //detailmessage.put("其他信息",content);

                                          detailmessage.put("基本信息", jsonArray);
                                          //-------------------------------------

                                          content = this.parseWindowprocessing(TRANSACTCODE);
                                          detailmessage.put("窗口办理", content);

                                          content = this.parseConditions_of_application(TRANSACTCODE);
                                          detailmessage.put("申请条件", content);

                                          content = this.parseLicensing_fees();
                                          detailmessage.put("许可收费", content);

                                          content = this.parseIntermediary_Services(TRANSACTCODE);
                                          detailmessage.put("中介服务", content);

                                          System.out.println(detailmessage);

                                      }
                                  }
                              }
                              ++currentPage;
                          }
                      }
                  }
                  //orderSort为2代表访问公共服务清单
                  if (orderSort.equals("2")){
                      int pages = 0;
                      int currentPage = 1;
                      while (pages != currentPage) {
                          System.out.println(currentPage);
                          String listUrl = "http://zwfw.hubei.gov.cn/web/bszn/zwgk/qzqd/getQzqdItemList.jspc";
                          JSONObject parmas = new JSONObject();
                          parmas.put("taskType","20");
                          parmas.put("regionCode", "420000000000");
                          parmas.put("areaGrade", "2");
                          parmas.put("currentPage", currentPage);
                          parmas.put("pageSize", "10");
                          parmas.put("onlyLocalLevel","1");
                          String listResponse = postFormReq(listUrl, new JSONObject(), parmas);
                          JSONObject responseObj = JSONObject.parseObject(listResponse);
                          if (responseObj.getString("status").equals("200")) {
                              pages = responseObj.getJSONObject("data").getJSONObject("pageList").getInteger("pages");
                              JSONArray list = responseObj.getJSONObject("data").getJSONObject("pageList").getJSONArray("records");
                              for (Object o1 : list) {    //获取必要信息
                                  String TASKCODE = ((JSONObject) o1).getString("TASKCODE");
                                  String TASKNAME = ((JSONObject) o1).getString("TASKNAME");
                                  //getQzqdSJYWList.jspc 访问该网页从而获取TRANSACTCODE
                                  String MiddleListUrl = "http://zwfw.hubei.gov.cn/web/bszn/zwgk/qzqd/getQzqdSJYWList.jspc";
                                  JSONObject MiddleParmasnext = new JSONObject();
                                  MiddleParmasnext.put("taskcode", TASKCODE);
                                  String listResponsenext = postFormReq(MiddleListUrl, new JSONObject(),MiddleParmasnext);
                                  JSONObject responseObjnext = JSONObject.parseObject(listResponsenext);
                                  JSONArray transactCodeList = responseObjnext.getJSONObject("data").getJSONArray("records");
                                  for (Object o2 : transactCodeList) {
                                      String TRANSACTCODE = ((JSONObject) o2).getString("TRANSACTCODE");
                                      //开始解析详情页

                                      JSONObject detailmessage = new JSONObject();

                                      JSONObject content = this.parseLegalbasis(TRANSACTCODE);
                                      detailmessage.put("设定依据", content);

                                      //content = this.parseLingprocess(TRANSACTCODE);
                                      //detailmessage.put("办理流程", content);

                                      content = this.parsePagecondition(TRANSACTCODE);
                                      detailmessage.put("受理条件", content);

                                      //用来将多个jsonobject对象进行组合---------------------------
                                      JSONArray jsonArray = new JSONArray();

                                      content = this.parseInfoExt(TRANSACTCODE);
                                      jsonArray.add(content);
                                      //detailmessage.put("基本信息",content);

                                      content = this.parseDocument(TRANSACTCODE);
                                      jsonArray.add(content);
                                      //detailmessage.put("审批结果信息",content);

                                      content = this.parseProcessingInformation(TRANSACTCODE);
                                      jsonArray.add(content);
                                      //detailmessage.put("办理信息",content);

                                      content = this.parseOther(TRANSACTCODE);
                                      jsonArray.add(content);
                                      //detailmessage.put("其他信息",content);

                                      detailmessage.put("基本信息", jsonArray);
                                      //-------------------------------------

                                      content = this.parseWindowprocessing(TRANSACTCODE);
                                      detailmessage.put("窗口办理", content);

                                      content = this.parseConditions_of_application(TRANSACTCODE);
                                      detailmessage.put("申请条件", content);

                                      content = this.parseLicensing_fees();
                                      detailmessage.put("许可收费", content);

                                      content = this.parseIntermediary_Services(TRANSACTCODE);
                                      detailmessage.put("中介服务", content);

                                      System.out.println(detailmessage);

                                  }
                              }
                          }
                          ++currentPage;
                      }

                  }
                  //orderSort为3代表访问中介服务事项清单
                  if (orderSort.equals("3")){
                      int pages = 0;
                      int currentPage = 1;
                      while (pages != currentPage) {
                          System.out.println(currentPage);
                          String ListUrl = "http://zwfw.hubei.gov.cn/web/bszn/zwgk/zjfw/zjitemList.jspc";
                          JSONObject parmas = new JSONObject();
                          parmas.put("region_code", "420000000000");
                          parmas.put("areaGrade", "2");
                          parmas.put("currentPage", currentPage);
                          parmas.put("pageSize", "10");
                          String listResponse = postFormReq(ListUrl, new JSONObject(), parmas);
                          JSONObject responseObj = JSONObject.parseObject(listResponse);
                          if (responseObj.getString("status").equals("200")) {
                              pages = responseObj.getJSONObject("data").getInteger("pages");
                              JSONArray list = responseObj.getJSONObject("data").getJSONArray("records");
                              //用来存储数据的数据结构
                              JSONArray JsonArray = new JSONArray();

                              for(Object o1 :list) {
                                  JSONObject MidLabel = new JSONObject();
                                  JSONObject res = new JSONObject();
                                  String RN = ((JSONObject)o1).getString("RN");
                                  res.put("中介服务事项名称",((JSONObject)o1).getString("ITEM_NAME"));
                                  res.put("涉及的审批事项名称",((JSONObject)o1).getString("RELITEM_NAME"));
                                  res.put("设定依据",((JSONObject)o1).getString("BASIS"));
                                  res.put("审批部门",((JSONObject)o1).getString("SHORT_NAME"));
                                  res.put("中介服务实施机构",((JSONObject)o1).getString("IMPLEMENT_AGENCY"));
                                  MidLabel.put("名称",RN);
                                  MidLabel.put("选项",res);
                                  System.out.println(MidLabel);
                              }



                          }
                          currentPage++;
                      }
                  }
                  if (orderSort.equals("4")){
                      String listUrl ="http://zwfw.hubei.gov.cn/webview/zwgk/sfqd/420000SF.html";
                      JSONObject headers = new JSONObject();
                      JSONObject parmas = new JSONObject();;
                      String listResponse =  getReq(listUrl, headers, parmas);
                      //解码html网页中文乱码的问题
                      String html = new String(listResponse.getBytes("ISO-8859-1"),"utf-8");
                      //解析xml文档,加载文档进内存,获取dom树--->Document
                      Document document = Jsoup.parse(html);
                      //根据document对象创建jxdocument对象
                      JXDocument jxDocument = new JXDocument(document);
                      //            使用XPath语法查询
                      //            查询所有的user标签,语法//
                      List<Object> jxNodes = jxDocument.sel("//user");
                  }
              }
          }
              return null;
  }
  private  JSONObject parseLegalbasis(String TRANSACTCODE){
      String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
      JSONObject detailparmas = new JSONObject();
      detailparmas.put("transactCode", TRANSACTCODE);
      detailparmas.put("type", "legalbasis");
      String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas);
      JSONObject detailobj = JSONObject.parseObject(detailResponse);
      JSONArray detaillist =JSONArray.parseArray(detailobj.getString("data"));
      //用来存储数据的数据结构
      List<JSONObject> list =new ArrayList<>();
      JSONObject NEW = new JSONObject();

      for(Object o:detaillist)
      {
          JSONObject res = new JSONObject();
          JSONObject object = new JSONObject();

          String NAME =  ((JSONObject)o).getString("NAME");
          res.put("号令",((JSONObject)o).getString("NUM"));
          res.put("颁布机构",((JSONObject)o).getString("IssueDept"));
          res.put("生效日期",((JSONObject)o).getString("EffectDay"));
          res.put("发布日期",((JSONObject)o).getString("EffectDay"));
          res.put("法规原文链接",((JSONObject)o).getString("Url"));
          res.put("条款内容",((JSONObject)o).getString("LawItems"));
          object.put(NAME,res);
          list.add(object);
      }
          NEW.put("文本",list);
          return NEW;
  }
  private  JSONObject parseLingprocess(String TRANSACTCODE) throws IOException {
        String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
        JSONObject detailparmas = new JSONObject();
        detailparmas.put("transactCode", TRANSACTCODE);
        detailparmas.put("type", "handlingprocess");
        String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas);
        JSONObject detailobj = JSONObject.parseObject(detailResponse);
        JSONArray detaillist =JSONArray.parseArray(detailobj.getString("data"));
        //存储数据的数据结构
        List<JSONObject> list =new ArrayList<>();
        JSONObject NEW = new JSONObject();

        String  specialType="";
        String  noExtendType="";
        StringBuffer strs = new StringBuffer();
        for(Object o:detaillist) {
            JSONObject object = new JSONObject();
            JSONObject res = new JSONObject();
            String NodeName = ((JSONObject) o).getString("NodeName");
            specialType = ((JSONObject) o).getString("SpecialType");
            if (specialType.equals("1")) {
                specialType = "延长审批";
            } else if (specialType.equals("2")) {
                specialType = "除延长审批之外的情况";
            } else {
                specialType = "无";
            }
            res.put("审查标准", ((JSONObject) o).getString("CheckContent"));
            res.put("办理结果", ((JSONObject) o).getString("Result"));
            res.put("是否为特殊程序", IsSpecial(((JSONObject) o).getString("IsSpecial")));
            String NoExtendType = ((JSONObject) o).getString("NoExtendType");
            if (StringUtils.isBlank(NoExtendType)) {
                noExtendType = "无";
                res.put("特殊程序种类", noExtendType);
            } else {
                String[] NoExtendTypelist = getNEType(NoExtendType);
                //将字符串数组拼接成字符串
                for (String s : NoExtendTypelist) {
                    strs.append(s);
                    strs.append(",");
                }
                res.put("特殊程序种类", strs);
            }
            if (IsSpecial(((JSONObject) o).getString("IsSpecial")).equals("否")) {
                specialType = "-";
                noExtendType = "-";
                res.put("特殊程序种类", noExtendType);
            }
            res.put("特殊程序类型", specialType);
            object.put(NodeName,res);
            list.add(object);
        }
        JSONObject handlingProceduresImage = new JSONObject();
        String orgImageUrl = "http://zwfw.hubei.gov.cn/web/lcfile/group1/M00/0A/35/wKhUBF-M_DKAQFglAAATYK0ceXw810.png";
        handlingProceduresImage.put("流程图原地址", orgImageUrl);
        handlingProceduresImage.put("minio地址", "没有内网 ---------");
        //handlingProceduresImage.put("minio地址", fileDownloadAndUploadMinioGet(orgImageUrl, "流程图.png"));
        NEW.put("办理流程图", handlingProceduresImage);
        NEW.put("文本",list);

        return NEW;
    }
  private  JSONObject parsePagecondition(String TRANSACTCODE) {
      //存储数据的数据结构
      JSONObject res = new JSONObject();
      String subjectIdsGr = "";

      String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
      JSONObject detailparmas = new JSONObject();
      detailparmas.put("transactCode", TRANSACTCODE);
      detailparmas.put("type", "info");
      String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas);
      JSONObject detailobj = JSONObject.parseObject(detailResponse);
      JSONArray detaillist = JSONArray.parseArray(detailobj.getString("data"));
      StringBuffer strs = new StringBuffer();
      for (Object o : detaillist) {
          String serviceCode = ((JSONObject) o).getString("serviceCode");
          String[] fwdx = getfwdx(serviceCode);
          for (String s : fwdx) {
              strs.append(s);
              strs.append(",");
          }
          res.put("服务对象", strs);
      }
      JSONObject detailparmasnext = new JSONObject();
      detailparmasnext.put("transactCode", TRANSACTCODE);
      detailparmasnext.put("type", "subject");
      detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmasnext);
      detailobj = JSONObject.parseObject(detailResponse);
      detaillist = JSONObject.parseArray(detailobj.getString("data"));
      String frzt = "";
      String zrzt = "";
      for (Object o1 : detaillist) {
          String SubjectType = ((JSONObject) o1).getString("SubjectType");
          if (StringUtils.isBlank(SubjectType)) {
              frzt = "";
              zrzt = "";
          } else {
              if (detaillist.size() > 1) {
                  if (SubjectType.equals("1")) {  //法人
                      subjectIdsGr = ((JSONObject) o1).getString("SubjectId");
                      frzt = getSxzt1(subjectIdsGr);
                  }
                  if (SubjectType.equals("0")) {
                      subjectIdsGr = ((JSONObject) o1).getString("SubjectId");
                      zrzt = getSxzt2(subjectIdsGr);

                  }
              } else {
                  if (SubjectType.equals("1")) {  //法人
                      subjectIdsGr = ((JSONObject) o1).getString("SubjectId");
                      frzt = getSxzt1(subjectIdsGr);
                      zrzt = "无";
                  }
                  if (SubjectType.equals("0")) {
                      subjectIdsGr = ((JSONObject) o1).getString("SubjectId");
                      zrzt = getSxzt2(subjectIdsGr);
                      frzt = "无";
                  }
              }
          }
      }
      res.put("面向法人事项主题", frzt);
      res.put("面向自然人事项主题", zrzt);
      JSONObject detailparmasnext2 = new JSONObject();
      detailparmasnext2.put("transactCode", TRANSACTCODE);
      detailparmasnext2.put("type", "condition");
      detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmasnext2);
      detailobj = JSONObject.parseObject(detailResponse);
      detaillist = JSONObject.parseArray(detailobj.getString("data"));
      for (Object o2 : detaillist) {
          res.put("申请条件",((JSONObject)o2).getString("AcceptCondition"));
      }
        return res;
    }
  private  JSONObject parseInfoExt(String TRANSACTCODE) {
        String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
        JSONObject detailparmas = new JSONObject();
        detailparmas.put("transactCode", TRANSACTCODE);
        detailparmas.put("type", "infoExt");
        String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas);
        JSONObject detailobj = JSONObject.parseObject(detailResponse);
        JSONArray detaillist =JSONArray.parseArray(detailobj.getString("data"));
        //存储数据的数据结构
        JSONObject res = new JSONObject();

        for(Object o:detaillist) {
            res.put("事项名称", ((JSONObject) o).getString("TransactName"));
            res.put("实施编码", ((JSONObject) o).getString("TaskCode"));
            res.put("业务编码", ((JSONObject) o).getString("TransactCode"));
            res.put("基本编码", ((JSONObject) o).getString("CatalogCode"));
            res.put("受理机构", ((JSONObject) o).getString("DeptName"));
            res.put("办理类型", ProjectType(((JSONObject) o).getString("ProjectType")));
            String DeptType = ((JSONObject) o).getString("DeptType");
            if (StringUtils.isBlank(DeptType)) {
                res.put("主体性质", "受委托组织");
            } else {
                if (DeptType.equals("1")) {
                    res.put("主体性质", "法定机关");
                }
                if (DeptType.equals("2")) {
                    res.put("主体性质", "授权组织");
                }
            }
            if (StringUtils.isBlank(((JSONObject) o).getString("EntrustName"))) {
                res.put("委托部门", "无");
            } else {
                res.put("委托部门", ((JSONObject) o).getString("EntrustName"));
            }
            String PowerSource = ((JSONObject) o).getString("PowerSource");
            if (StringUtils.isBlank(PowerSource)) {
                res.put("权利来源", "无");
            } else {
                PowerSource = getPowerSource(PowerSource);
                res.put("权利来源", PowerSource);
            }

        }


        JSONObject detailparmasnext = new JSONObject();
        //打开新的JSPC获取所需要的信息
        detailparmasnext.put("transactCode", TRANSACTCODE);
        detailparmasnext.put("type", "info");
        String Response = postFormReq(detailUrl, new JSONObject(), detailparmasnext);
        JSONObject content = JSONObject.parseObject(Response);
        JSONArray data = JSONObject.parseArray(content.getString("data"));
        String AnticipateDay = "";
        String PromiseDay = "";
        String TransactionUseLevel = "";
        for (Object o : data) {
            String TaskType = ((JSONObject) o).getString("TaskType");
            res.put("事项类型", getTaskType(TaskType));
            AnticipateDay = ((JSONObject) o).getString("AnticipateDay");
            PromiseDay = ((JSONObject) o).getString("PromiseDay");
            TransactionUseLevel = ((JSONObject) o).getString("TransactionUseLevel");
            if (StringUtils.isBlank(TransactionUseLevel)) {
                String UseLevel = ((JSONObject) o).getString("UseLevel");
                if (StringUtils.isBlank(UseLevel)) {
                    res.put("行使层级", "无");

                } else {
                    String[] UseLevellist = getxscj(UseLevel);
                    StringBuffer useLevel = new StringBuffer();
                    for (String s : UseLevellist) {
                        useLevel.append(s);
                        useLevel.append(",");
                    }
                    res.put("行使层级", useLevel);
                }
            } else {
                String[] TUseLevellist = getxscj(TransactionUseLevel);
                StringBuffer TUseLevel = new StringBuffer();
                for (String s : TUseLevellist) {
                    TUseLevel.append(s);
                    TUseLevel.append(",");
                }
                res.put("行使层级", TUseLevel);
            }
        }
        AnticipateDay = AnticipateDay + "个工作日";
        res.put("法定办结时限", AnticipateDay);

        PromiseDay = PromiseDay + "个工作日";
        res.put("承诺办结时限", PromiseDay);

        JSONObject detailparmasnext2 = new JSONObject();
        detailparmasnext2.put("transactCode", TRANSACTCODE);
        detailparmasnext2.put("type", "window");
        Response = postFormReq(detailUrl, new JSONObject(), detailparmasnext2);
        content = JSONObject.parseObject(Response);
        data = JSONObject.parseArray(content.getString("data"));
        String NetworkDepth = "";
        for (Object o : data) {
            NetworkDepth = ((JSONObject) o).getString("NetworkDepth");
        }
        String[] NetworkDepthlist = getWbsd(NetworkDepth);
        StringBuffer strs = new StringBuffer();
        for (String s : NetworkDepthlist) {
            strs.append(s);
            strs.append(",");
        }
        res.put("网办深度", strs);
        return res;
    }
  private  JSONObject parseDocument(String TRANSACTCODE) throws IOException {
       String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
       JSONObject detailparmas = new JSONObject();
       detailparmas.put("transactCode", TRANSACTCODE);
       detailparmas.put("type", "document");
       String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas);
       JSONObject detailobj = JSONObject.parseObject(detailResponse);
       JSONArray detaillist =JSONArray.parseArray(detailobj.getString("data"));
       //存储数据的数据结构
       JSONObject res = new JSONObject();

       for (Object o : detaillist) {
           String isnot =((JSONObject)o).getString("isnot");
           if((!StringUtils.isBlank(isnot)) && isnot.equals("0")){
               String AllowName=((JSONObject)o).getString("AllowName");
               if(!StringUtils.isBlank(AllowName)) {
                   String samplePath =((JSONObject)o).getString("samplePath");
                   if(StringUtils.isBlank(samplePath)) {
                       res.put("审批结果",AllowName);
                   }
                   else
                   {
                       JSONObject handlingProceduresImage = new JSONObject();
                       String orgImageUrl = "http://zwfw.hubei.gov.cn/web/lcfile/" + samplePath;
                       handlingProceduresImage.put("准许下载样本原地址", orgImageUrl);
                       handlingProceduresImage.put("minio地址", "需要内网 --------------------------");
                       //handlingProceduresImage.put("minio地址", downLoadFileUpLoadMinio(orgImageUrl, "下载样本.png"));
                       handlingProceduresImage.put("文本",AllowName);
                       res.put("审批结果",handlingProceduresImage);
                   }
               }
               else
               {
                   res.put("审批结果","");
               }
               String AllowType=((JSONObject)o).getString("AllowType");
               if(!StringUtils.isBlank(AllowType)) {
                   AllowType=getType(AllowType);
                   res.put("审批结果类型", AllowType);
               }
               else {res.put("审批结果类型","无");}
               if(detaillist.size()==1) {
                   res.put("不准予结果名称","无");
                   res.put("不准予结果类型","无");
                   res.put("不予审批结果","无");
               }

           }
           else
           {
               String NotAllowedSample =((JSONObject)o).getString("NotAllowedSample");
               String NotAllowedName=((JSONObject)o).getString("NotAllowedName");
               if(!StringUtils.isBlank(NotAllowedName)) {
                   if(StringUtils.isBlank(NotAllowedSample) || NotAllowedSample.equals("undefined/undefined")){
                       res.put("审批不通过结果",NotAllowedName);
                   }
                   else
                   {
                       JSONObject handlingProceduresImage = new JSONObject();
                       String orgImageUrl = "http://zwfw.hubei.gov.cn/web/lcfile/" + NotAllowedSample;
                       handlingProceduresImage.put("下载样本原地址", orgImageUrl);
                       handlingProceduresImage.put("minio地址", "需要内网 ---------------------");
                       //handlingProceduresImage.put("minio地址", downLoadFileUpLoadMinio(orgImageUrl, "下载样本.png"));
                       handlingProceduresImage.put("文本",NotAllowedName);
                       res.put("审批不通过结果",handlingProceduresImage);
                   }
               }
               else
               {
                   res.put("审批不通过结果","");
               }
               String NotAllowedType = ((JSONObject)o).getString("NotAllowedName");
               if(StringUtils.isBlank(NotAllowedType)) {
                   res.put("审批不通过结果类型","无");
               }
               else
               {
                   NotAllowedType=getType(NotAllowedType);
                   res.put("审批不通过结果类型",NotAllowedType);
               }
               if (detaillist.size() == 1) {
                   res.put("审批结果名称","无");
                   res.put("准予许可结果类型","无");
               }
           }
       }


        return res;
    }
  private  JSONObject parseProcessingInformation(String TRANSACTCODE) {
        //存储数据的数据结构
        JSONObject res = new JSONObject();

        String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
        JSONObject detailparmas = new JSONObject();
        detailparmas.put("transactCode", TRANSACTCODE);
        detailparmas.put("type", "infoExt");
        String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas);
        JSONObject detailobj = JSONObject.parseObject(detailResponse);
        JSONArray detaillist = JSONArray.parseArray(detailobj.getString("data"));
        for (Object o : detaillist) {   ;
            String HandleArea = ((JSONObject) o).getString("HandleArea");
            if (StringUtils.isBlank(HandleArea)) {
                res.put("通办范围", "无");
            } else
            {
                res.put("通办范围", getHandleArea(HandleArea));
            }
            String IsCloseToHome = ((JSONObject) o).getString("IsCloseToHome");
            if (StringUtils.isBlank(IsCloseToHome)) {
                res.put("是否就近办理", "否");
            } else
            {
                res.put("是否就近办理", "是");
            }
            String HandleForm = ((JSONObject) o).getString("HandleForm");
            if (StringUtils.isBlank(HandleForm)) {
                res.put("办理形式", "无");
            }
            else
            {
                StringBuffer strs = new StringBuffer();
                String[] fwdx = getBLstyle(HandleForm);
                for (String s : fwdx) {
                    strs.append(s);
                    strs.append(",");
                }

                res.put("办理形式", strs);
            }

        }
        JSONObject detailparmasnext =  new JSONObject();
        detailparmasnext.put("transactCode", TRANSACTCODE);
        detailparmasnext.put("type", "info");
        detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmasnext);
        detailobj = JSONObject.parseObject(detailResponse);
        detaillist = JSONArray.parseArray(detailobj.getString("data"));
        for (Object o : detaillist) {
            String IsOnline = ((JSONObject)o).getString("IsOnline");
            if(StringUtils.isBlank(IsOnline)) {
                res.put("是否网办","否");
            }
            else
            {
                res.put("是否网办","是");
            }
        }
        res.put("网办地址","http://zwfw.hubei.gov.cn");
        JSONObject detailparmasnext2 =  new JSONObject();
        detailparmasnext2.put("transactCode", TRANSACTCODE);
        detailparmasnext2.put("type", "window");
        detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmasnext2);
        detailobj = JSONObject.parseObject(detailResponse);
        detaillist = JSONArray.parseArray(detailobj.getString("data"));
        for (Object o : detaillist)
        {
            String IsSchedule = ((JSONObject)o).getString("IsSchedule");
            if(StringUtils.isBlank(IsSchedule))
            {
                res.put("是否支持网上预约办理","不支持");

            }
            else
            {
                res.put("是否支持网上预约办理","支持");
            }

        }

        return res;
    }
  private  JSONObject parseOther(String TransactCode) {
        JSONObject res = new JSONObject();
        String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
        JSONObject detailparmas = new JSONObject();
        detailparmas.put("transactCode", TransactCode);
        detailparmas.put("type", "infoExt");
        String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas);
        JSONObject detailobj = JSONObject.parseObject(detailResponse);
        JSONArray detaillist = JSONArray.parseArray(detailobj.getString("data"));
        for (Object o:detaillist) {
            String OtherDept = ((JSONObject) o).getString("OtherDept");
            if(StringUtils.isBlank(OtherDept)){
                res.put("联办机构","无");
            }
            else
            {
                res.put("联办机构",OtherDept);
            }
            String LimitExplain = ((JSONObject) o).getString("LimitExplain");
            if(StringUtils.isBlank(LimitExplain)) {
                res.put("数量限制","无");
            }
            else
            {
                res.put("数量限制",LimitExplain);
            }
        }
        JSONObject detailparmasnext = new JSONObject();
        detailparmasnext.put("transactCode", TransactCode);
        detailparmasnext.put("type", "window");
        detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmasnext);
        detailobj = JSONObject.parseObject(detailResponse);
        detaillist = JSONArray.parseArray(detailobj.getString("data"));
        for (Object o:detaillist) {
            String IsServiceTerminals = ((JSONObject) o).getString("IsServiceTerminals");
            if(IsServiceTerminals.equals("1")) {
                res.put("自助终端办理","支持");
            }
            else
            {
                res.put("自助终端办理","不支持");
            }
            String IsMobileTerminal = ((JSONObject) o).getString("IsMobileTerminal");
            if(StringUtils.isBlank(IsMobileTerminal)) {
                res.put("移动端形式","不支持");
            }
            else
            {
                if(IsMobileTerminal.equals("1")) {
                    String MobileTerminalType = ((JSONObject) o).getString("MobileTerminalType");
                    if(StringUtils.isBlank(MobileTerminalType) || MobileTerminalType.equals("[]")) {
                        res.put("移动端形式","不支持");
                    }
                    else
                    {
                        StringBuffer strs = new StringBuffer();
                        String[] fwdx = getAppType(MobileTerminalType);
                        for (String s : fwdx) {
                            strs.append(s);
                            strs.append(",");
                        }
                        res.put("移动端形式", strs);
                    }
                }
                else
                {
                    res.put("移动端形式","不支持");
                }
            }
            String MobileTerminalDepth = ((JSONObject) o).getString("MobileTerminalDepth");
            if(StringUtils.isBlank(MobileTerminalDepth) || MobileTerminalDepth.equals("[]")) {
                res.put("移动服务内容","无");
            }
            else
            {
                StringBuffer strs = new StringBuffer();
                String[] fwdx = getAppDepth(MobileTerminalDepth);
                for (String s : fwdx) {
                    strs.append(s);
                    strs.append(",");
                }
                res.put("移动服务内容", strs);
            }
        }
        JSONObject detailparmasnext2 = new JSONObject();
        detailparmasnext2.put("transactCode", TransactCode);
        detailparmasnext2.put("type", "country");
        detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmasnext2);
        detailobj = JSONObject.parseObject(detailResponse);
        detaillist = JSONArray.parseArray(detailobj.getString("data"));
        for (Object o :detaillist) {
            String AppIsSingleLogin = ((JSONObject) o).getString("AppIsSingleLogin");
            if(StringUtils.isBlank(AppIsSingleLogin) || !AppIsSingleLogin.equals("1")) {
                res.put("移动端是否对接单点登录","否");
            }
            else
            {
                res.put("移动端是否对接单点登录","是");
            }
            String MobileTerminalUrl = ((JSONObject) o).getString("MobileTerminalUrl");
            if(StringUtils.isBlank(MobileTerminalUrl) || !MobileTerminalUrl.equals("1")) {
                res.put("移动端是否对接单点登录","否");
            }
            else
            {
                res.put("移动端是否对接单点登录","是");
            }
        }
        res.put("移动端办理地址","无");
        res.put("计算机端是否对接单点登录","无");
        res.put("计算机端办理地址","无");
        res.put("是否支持网上支付","不支持");
        res.put("是否信用监督","无");
        res.put("是否年审年检","无");
        res.put("是否物流","无");
        return res;
    }
  private JSONObject parseWindowprocessing(String TransactCode){
        JSONObject res = new JSONObject();
        String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
        JSONObject detailparmas1 = new JSONObject();
        detailparmas1.put("transactCode", TransactCode);
        detailparmas1.put("type", "window");
        String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas1);
        JSONObject detailobj = JSONObject.parseObject(detailResponse);
        JSONArray detaillist = JSONArray.parseArray(detailobj.getString("data"));
        for(Object o:detaillist) {
            String EnterHall = ((JSONObject) o).getString("EnterHall");
            if(StringUtils.isBlank(EnterHall) || !EnterHall.equals("1")){
                res.put("是否进驻服务大厅","否");
            }
            else
            {
                res.put("是否进驻服务大厅","是");
            }
            String NAME = ((JSONObject) o).getString("NAME");
            if(StringUtils.isBlank(NAME)) {
                res.put("办事大厅名称","无");
            }
            else
            {
                res.put("办事大厅名称",NAME);
            }

            String OfficeHour = ((JSONObject) o).getString("OfficeHour");
            if(StringUtils.isBlank(OfficeHour)) {
                res.put("办理时间","无");
            }
            else
            {
                res.put("办理时间",OfficeHour);
            }

            String ADDRESS = ((JSONObject) o).getString("ADDRESS");
            if(StringUtils.isBlank(ADDRESS)) {
                res.put("办理地点","无");
            }
            else
            {
                res.put("办理地点", ADDRESS);
            }

            String TransactWindow = ((JSONObject) o).getString("TransactWindow");
            if(StringUtils.isBlank(TransactWindow)) {
                res.put("办理窗口","无");
            }
            else
            {
                res.put("办理窗口", TransactWindow);
            }
            String PHONE = ((JSONObject) o).getString("PHONE");
            if(StringUtils.isBlank(PHONE)) {
                res.put("办公电话","无");
            }
            else
            {
                res.put("办公电话", PHONE);
            }
//            "http://zwfw.hubei.gov.cn/web/lcfile/"

            String MapImage = ((JSONObject) o).getString("MapImage");
            if(StringUtils.isBlank(MapImage) || MapImage.equals("undefined/undefined")) {
                if(StringUtils.isBlank(ADDRESS)){
                    res.put("办理地点地图","无");
                }
                else
                {
                    res.put("办理地点地图",ADDRESS);
                }
            }
            else
            {
                JSONObject handlingProceduresImage = new JSONObject();
                String orgImageUrl = "http://zwfw.hubei.gov.cn/web/lcfile/" + MapImage;
                handlingProceduresImage.put("下载样本原地址", orgImageUrl);
                handlingProceduresImage.put("minio地址","需要内网 ----------------------");
                //handlingProceduresImage.put("minio地址", downLoadFileUpLoadMinio(orgImageUrl, "办理地点地图.png"));
                res.put("办理地点地图", handlingProceduresImage);
            }
            String TrafficGuide = ((JSONObject) o).getString("TrafficGuide");
            String MetroGuide = ((JSONObject) o).getString("MetroGuide");
            if(StringUtils.isBlank(TrafficGuide)) {
                if(StringUtils.isBlank(MetroGuide)) {
                    res.put("公交","无");
                    res.put("地铁","无");

                }
                res.put("公交","无");
                res.put("地铁",MetroGuide);
            }
            res.put("公交",TrafficGuide);
            res.put("地铁",MetroGuide);

            String HandlingTimes = ((JSONObject) o).getString("HandlingTimes");
            if(!StringUtils.isBlank(HandlingTimes)) {
                switch (HandlingTimes) {
                    case "1":
                        HandlingTimes = "跑一次";
                        break;
                    case "2":
                        HandlingTimes = "跑多次";
                        break;
                    case "0":
                        HandlingTimes = "跑零次";
                        break;
                }
                res.put("到办事现场次数",HandlingTimes);
            }
            res.put("到办事现场次数","");
            String LimitSceneExplain = ((JSONObject) o).getString("LimitSceneExplain");
            if(StringUtils.isBlank(LimitSceneExplain)) {
                res.put("必须现场办理原因说明","无");
            }
            res.put("必须现场办理原因说明",LimitSceneExplain);
        }
        return res;

    }
  private JSONObject parseConditions_of_application(String TransactCode) {
        JSONObject NEW = new JSONObject();
        List<JSONObject> list =new ArrayList<>();

        String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
        JSONObject detailparmas1 = new JSONObject();
        detailparmas1.put("transactCode", TransactCode);
        detailparmas1.put("type", "material");
        String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas1);
        JSONObject detailobj = JSONObject.parseObject(detailResponse);
        JSONArray detaillist = JSONArray.parseArray(detailobj.getString("data"));
        for (Object o:detaillist) {
            JSONObject res = new JSONObject();
            JSONObject object = new JSONObject();
            String alias = ((JSONObject) o).getString("alias");
            String materialType = ((JSONObject) o).getString("materialType");
            res.put("材料分类",getClfl(materialType));
            String OriginalRequired = ((JSONObject) o).getString("OriginalRequired");
            if(StringUtils.isBlank(OriginalRequired))
            {
                res.put("原件要求","");
            }
            res.put("原件要求" , OriginalRequired.equals("1")  ? "核验" : OriginalRequired.equals("2") ? "存档":"");
            String OriginalNumber = ((JSONObject) o).getString("OriginalNumber");
            OriginalNumber = OriginalNumber+"份";
            res.put("原件" , OriginalNumber);
            String form = ((JSONObject) o).getString("form");
            if(form.equals("1"))
            {res.put("材料形式","纸质");}
            if(form.equals("2"))
            {res.put("材料形式","电子");}
            if(form.equals("3"))
            {res.put("材料形式","纸质电子");}
            //"http://zwfw.hubei.gov.cn/web/lcfile/"
            String ExampleGuidPath = ((JSONObject) o).getString("ExampleGuidPath");
            if(!StringUtils.isBlank(ExampleGuidPath))
            {
                JSONObject handlingProceduresImage = new JSONObject();
                String orgImageUrl = "http://zwfw.hubei.gov.cn/web/lcfile/" + ExampleGuidPath;
                handlingProceduresImage.put("查看实例原地址", orgImageUrl);
                handlingProceduresImage.put("minio地址","需要内网-----------------------");
                //handlingProceduresImage.put("minio地址", downLoadFileUpLoadMinio(orgImageUrl, "办理地点地图.png"));
                res.put("查看实例", handlingProceduresImage);
            }
            else {res.put("查看实例","无");}
            String sourceType = ((JSONObject) o).getString("sourceType");
            if(StringUtils.isBlank(sourceType))
            {res.put("来源渠道","无");}
            else {res.put("来源渠道",getSourceType(sourceType));}
            String sourceExplain = ((JSONObject) o).getString("sourceExplain");
            if(StringUtils.isBlank(sourceExplain))
            {res.put("来源说明","无");}
            else {res.put("来源说明",sourceExplain);}
            String materialSpec = ((JSONObject) o).getString("materialSpec");
            if(StringUtils.isBlank(materialSpec))
            {res.put("材料规格","无");}
            else {res.put("材料规格",materialSpec);}
            String FillExplain = ((JSONObject) o).getString("FillExplain");
            if(StringUtils.isBlank(FillExplain))
            {res.put("填表须知","无");}
            else {res.put("填表须知",FillExplain);}
            String AcceptStand = ((JSONObject) o).getString("AcceptStand");
            if(StringUtils.isBlank(AcceptStand))
            {res.put("材料受理标准","无");}
            else {res.put("材料受理标准",AcceptStand);}
            object.put(alias,res);
            list.add(object);
        }
        NEW.put("文本",list);
        return NEW;
    }
  private JSONObject parseLicensing_fees(){
        JSONObject res = new JSONObject();
        res.put("许可收费","无");
        return  res;
    }
  private JSONObject parseIntermediary_Services(String TransactCode){
        JSONObject res = new JSONObject();
        String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
        JSONObject detailparmas1 = new JSONObject();
        detailparmas1.put("transactCode", TransactCode);
        detailparmas1.put("type", "agent");
        String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas1);
        JSONObject detailobj = JSONObject.parseObject(detailResponse);
        JSONArray detaillist = JSONArray.parseArray(detailobj.getString("data"));
        if(detaillist.size()>0) {
            for (Object o : detaillist) {
                String name = ((JSONObject) o).getString("name");
                String ServiceType = ((JSONObject) o).getString("ServiceType");

                if(StringUtils.isBlank(name)) {
                    if(StringUtils.isBlank(ServiceType)) {
                        res.put("中介服务事项名称","无");
                    }
                    else
                    {
                        res.put("中介服务事项名称",ServiceType);
                    }
                }
                else
                {
                    res.put("中介服务事项名称",name);
                }

                String handle_url = ((JSONObject) o).getString("handle_url");
                String ServiceUrl = ((JSONObject) o).getString("ServiceUrl");
                if(StringUtils.isBlank(handle_url)) {
                    if(StringUtils.isBlank(ServiceUrl)) {
                        res.put("中介服务查询网址","无");

                    }
                    else { res.put("中介服务查询网址",ServiceUrl);}

                }
                else
                {
                    res.put("中介服务查询网址",handle_url);
                }

            }
        }
        return res;
    }
          public static void main (String[]args){
              SpiderHBZW spiderHBZW = new SpiderHBZW();
              spiderHBZW.getDataDemo();
          }

  }